package com.itheima;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailDemo {

    public static void main(String[] args) throws Exception {

        //1.设置邮件相关属性
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host"," smtp.126.com");
        properties.setProperty("mail.smtp.auth","true");

        //2.获取邮件会话(实例)对象
        Session session = Session.getInstance(properties);

        //3.创建一个消息对象
        Message message = new MimeMessage(session);

        //4.封装消息
           //发送方(需要提供秘钥)
        message.setFrom(new InternetAddress("lemon3150@126.com"));
           //设置邮件容器发送的模式
        message.setRecipient(Message.RecipientType.TO,new InternetAddress("554243044@qq.com"));
        message.setSubject("test Email"); //设置邮件主题
        message.setText("hello lemon"); //邮件内容

       //5.发送
        Transport smtp = session.getTransport("smtp");//发送邮件的协议
        //设置发送邮箱的协议,发送邮箱名称,发送邮箱的秘钥
        smtp.connect("smtp.126.com","lemon3150@126.com","guoguo111");
         //发送
        smtp.sendMessage(message,message.getAllRecipients());
        smtp.close();//关闭资源
    }
}
